package com.kins.so.knowledgebase.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.kins.so.chatassistant.utils.CommonUtils;
import com.kins.so.knowledgebase.entity.DocCheckLog;
import com.kins.so.knowledgebase.mapper.DocCheckLogMapper;
import com.kins.so.knowledgebase.service.IDocCheckLogService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description:
 * @Author: 申科
 * @Date:2025/11/26 17:26
 */

public class DocCheckLogServiceImpl extends ServiceImpl<DocCheckLogMapper, DocCheckLog> implements IDocCheckLogService {
    @Autowired
    CommonUtils commonUtils;

    @Autowired SystemCommonService commonService;

    public PageInfo<DocCheckLog> findCustomerSimDocList<Map<String,Object> paramMap>{
        int pageNum = MapUtils.getIntValue(paramMap, "pageNum",);
        int pageSize = MapUtils.getIntValue(paramMap, "pageSize");
        SysOrganization org = commonService.getParentOrgByLevel(MapUtils.getString(commonUtils.getUser(),"currInstCd"));
        PageHelper.startPage(pageNum, pageSize);
        paramMap.put("orgCode",org.getOrgCode());
        List<DocCheckLog> docCheckLogList = this.baseMapper.findCustomerSimDocList(paramMap);
        list.forEach(item->{
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cust_no",item.getCustNo());
            queryWrapper.orderByDesc("create_date");
            List<DocCheckLog> queryData = this.list(queryWrapper);
            queryData.forEach(data->{
                if(item.getCreateDate()==null){
                    item.setCreateDate(data.getCreateDate());
                }
                if(!"1".equals(item.getIsSimilarity())){
                    try{
                        item.setIsSimilarity("0");
                        List checkRstls = JSON.parseArray(data.getCheckResult());
                        if(checkRstls.size()>){
                            item.setIsSimilarity("1");
                        }
                    }catch (Exception e){
                        logger.error("解析检查结果异常",e);
                    }
                }
                
            }
        }
        return new PageInfo<>(list);
    }


    @Override
    public PageInfo<DocCheckLog> findPage(Map<String,Object> paramMap){
        int pageNum = MapUtils.getIntValue(paramMap, "pageNum",);
        int pageSize = MapUtils.getIntValue(paramMap, "pageSize");
        SysOrganization org = commonService.getParentOrgByLevel(MapUtils.getString(commonUtils.getUser(),"currInstCd"));
        PageHelper.startPage(pageNum, pageSize);
        String checkType = MapUtils.getString(paramMap, "checkType");
        String checkStatus = MapUtils.getString(paramMap, "checkStatus");
        String custNo = MapUtils.getString(paramMap, "custNo");
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if(StringUtil.isNotEmpty(checkType)){
            if(DocCheckLog.checkType_01.equals(checkType)){
                queryWrapper.and(wrapper->wrapper.eq("check_type",DocCheckLog.checkType_01).or().eq("check_type",DocCheckLog.checkType_02));

            }else{
                queryWrapper.eq("check_type",checkType);
            }
        }
        if(StringUtil.isNotEmpty(checkStatus)){
            queryWrapper.eq("check_status",checkStatus);
        }
        if(StringUtil.isNotEmpty(custNo)){
            queryWrapper.eq("cust_no",custNo);
        }
        queryWrapper.orderByDesc("create_date");
        List<DocCheckLog> list = this.list(queryWrapper);
        return new PageInfo<>(list);
    }
}
